#! /usr/bin/env python
# ______________________________________________________________________
"""PyPgen Script

Usage:
 % PyPgen [options] [source file]

Jonathan Riehl

$Id$
"""
# ______________________________________________________________________
# Module imports

import sys, getopt, pprint
from basil.lang.python import DFAParser
from basil.parsing import PgenParser, PyPgen

# ______________________________________________________________________
# Module data

pythonFmtStr = """#! /usr/bin/env python
# ______________________________________________________________________
# Automatically generated by PyPgen.  Edit at your own risk.
# ______________________________________________________________________
# Module imports

from basil.parsing import PyPgen

# ______________________________________________________________________
# Grammar object

grammarObj = %s

# ______________________________________________________________________

def getParser (tokenizerFactory = None):
    return PyPgen.PyPgenParser(grammarObj, tokenizerFactory)

# ______________________________________________________________________

if __name__ == '__main__':
    PyPgen.parserMain(grammarObj)

# ______________________________________________________________________
# End of %s
"""

# ______________________________________________________________________
# Main routine

def main ():
    # ____________________________________________________________
    srcFile = None
    destFile = None
    destLang = 'Python'
    addAccel = False
    pgenKWs = {}
    # ____________________________________________________________
    opts, args = getopt.getopt(sys.argv[1:], "aco:p:")
    for (opt_flag, opt_arg) in opts:
        if opt_flag == "-a":
            addAccel = True
        elif opt_flag == "-c":
            destLang = "C"
        elif opt_flag == "-o":
            destFile = opt_arg
        elif opt_flag == "-p":
            split_opt_arg = opt_arg.split("=")
            if len(split_opt_arg) == 1:
                pgenKWs[opt_arg] = True
            else:
                for kw in split_opt_arg[:-1]:
                    pgenKWs[kw] = split_opt_arg[-1]
    if srcFile == None:
        if len(args) < 1:
            print __doc__
            sys.exit(-1)
        else:
            srcFile = args[0]
    # ____________________________________________________________
    grammarST = PgenParser.parseFile(srcFile)
    parser = PyPgen.buildParser(grammarST, **pgenKWs)
    grammarObj = parser.grammarObj
    if addAccel:
        # XXX Add accelerators
        grammarObj = DFAParser.addAccelerators(grammarObj)
    if destFile == None:
        destFile = "parser"
        destFileObj = sys.stdout
    else:
        destFileObj = open(destFile,"w")
    if destLang == "Python":
        destFileObj.write(pythonFmtStr % (pprint.pformat(grammarObj),
                                          destFile))
        destFileObj.close()
    else:
        print "Unsupported output language: %s" % destLang

# ______________________________________________________________________

if __name__ == "__main__":
    main()

# ______________________________________________________________________
# End of PyPgen
